package com.cr.sync.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.cr.common.core.redis.RedisCache;
import com.cr.common.utils.DateUtils;
import com.cr.sync.domain.TbSyncProductionCertificate;
import com.cr.sync.domain.vo.CountVo;
import com.cr.sync.mapper.TbSyncProductionCertificateMapper;
import com.cr.sync.service.ITbSyncProductionCertificateService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * 远程数据-合格证情况Service业务层处理
 * 
 * @author tang
 * @date 2025-08-29
 */
@Service
public class TbSyncProductionCertificateServiceImpl implements ITbSyncProductionCertificateService 
{
    @Resource
    private TbSyncProductionCertificateMapper tbSyncProductionCertificateMapper;

    @Resource
    private RedisCache redisCache;

    @Override
    public void insertBatch(List<TbSyncProductionCertificate> data) {
//        tbSyncProductionCertificateMapper.deleteBatch(data);
        tbSyncProductionCertificateMapper.insertBatch(data);
    }

    @Override
    public Map<String, Object> getLsdIndexCount(String area) {

        // 1、总数
        int today = tbSyncProductionCertificateMapper.selectDateCount(DateUtils.getDate());
        int all = tbSyncProductionCertificateMapper.selectAll();
        int clazz = tbSyncProductionCertificateMapper.selectAllProductClazz();
        Map<String, Integer> info = new HashMap<>();
        info.put("all", all);
        info.put("today", today);
        info.put("clazz", clazz);

        // 2、开具趋势
        /*
        List<String> pastDays = DateUtils.getPastDays(7);
        List<Map<String, Object>> trendCount = new ArrayList<>();
        for (int i = 0; i < pastDays.size(); i ++) {
            String date = pastDays.get(i);
            int todayCount = tbSyncProductionCertificateMapper.selectDateCount(date);
            int historyCount = tbSyncProductionCertificateMapper.selectHistoryCount(date);

            Map<String, Object> map = new HashMap<>();
            map.put("date", date);
            map.put("todayCount", todayCount);
            map.put("historyCount", historyCount);
            trendCount.add(map);
        }
        */

        List<String> pastYears = DateUtils.getPastYears(7);
        List<Map<String, Object>> trendCount = new ArrayList<>();
        for (int i = 0; i < pastYears.size(); i ++) {
            String date = pastYears.get(i);
            int todayCount = tbSyncProductionCertificateMapper.selectYearCount(date);
            int historyCount = tbSyncProductionCertificateMapper.selectHistoryYearCount(date);

            Map<String, Object> map = new HashMap<>();
            map.put("date", date);
            map.put("todayCount", todayCount);
            map.put("historyCount", historyCount);
            trendCount.add(map);
        }

        // 3、产品开具排行
        List<CountVo> top10 = tbSyncProductionCertificateMapper.selectProductTop();
        // 4、地区开具数据占比
        List<CountVo> areaCount = tbSyncProductionCertificateMapper.selectAreaCount();

        Map<String, Object> result = new HashMap<>();
        result.put("info", info);
        result.put("trendCount", trendCount);
        result.put("top10", top10);
        result.put("areaCount", areaCount);

        redisCache.setCacheObject("lsdCount:cert", JSON.toJSONString(result), 1, TimeUnit.DAYS);
        return result;
    }

    @Override
    public JSONObject getLsdIndexCountInfo(String area) {
        String cacheObject = redisCache.getCacheObject("lsdCount:cert");
        JSONObject jsonObject = JSON.parseObject(cacheObject);
        if (jsonObject == null || jsonObject.isEmpty()) {
            Map<String, Object> lsdIndexCount = getLsdIndexCount(area);
            jsonObject = JSON.parseObject(JSON.toJSONString(lsdIndexCount));
        }
        return jsonObject;
    }
}
